# Contibutor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=qt5-3d
pkgbase="mingw-w64-${_realname}"
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-debug")
pkgver=5.15.10
pkgrel=1
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
pkgdesc="C++ and QML APIs for easy inclusion of 3D graphics (mingw-w64)"
url='https://www.qt.io/'
license=('GPL3' 'LGPL' 'FDL' 'custom')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-qt5-declarative"
             "${MINGW_PACKAGE_PREFIX}-assimp"
             "${MINGW_PACKAGE_PREFIX}-qt5-imageformats"
             "${MINGW_PACKAGE_PREFIX}-qt5-gamepad"
             "${MINGW_PACKAGE_PREFIX}-vulkan-headers"
             "rsync")
_pkgfn="${_realname/5-/}-everywhere-opensource-src-${pkgver}"
source=("https://download.qt.io/official_releases/qt/${pkgver%.*}/$pkgver/submodules/$_pkgfn.tar.xz")
sha256sums=('225c06128b9b3574fd96d87890bd28c856198c157b91619e1e8ea8de41958c5f')

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  local -a _simd
  if [[ ${CARCH} != aarch64 ]]; then
    _simd+=("-qt3d-simd" "sse2")
  fi

  qmake ../${_pkgfn/opensource-/} -- \
    -system-assimp \
    "${_simd[@]}"

  make
}

check() {
  cd build-${MSYSTEM}
  make check -j1 -k
}

package_qt5-3d() {
  depends=("${MINGW_PACKAGE_PREFIX}-qt5-declarative"
           "${MINGW_PACKAGE_PREFIX}-assimp")
  optdepends=("${MINGW_PACKAGE_PREFIX}-qt5-imageformats"
              "${MINGW_PACKAGE_PREFIX}-qt5-gamepad")
  groups=("${MINGW_PACKAGE_PREFIX}-qt5")

  cd build-${MSYSTEM}

  # Fix Makefiles
  local PREFIX_WIN=$(cygpath -am ${MINGW_PREFIX})
  find "${srcdir}/build-${MSYSTEM}" -type f \( -name 'Makefile*.Release' \) \
      -exec sed -i -e "s|${PREFIX_WIN:0:2}\$(INSTALL_ROOT|\$(INSTALL_ROOT|g" {} \;
  find "${srcdir}/build-${MSYSTEM}" -type f \( -name 'Makefile*.Release' \) \
      -exec sed -i -e "s|)${PREFIX_WIN:2}|)${MINGW_PREFIX}|g" {} \;

  make INSTALL_ROOT="${pkgdir}" install

  install -d "$pkgdir${MINGW_PREFIX}"/share/licenses/${_realname}
  install -Dm644 $srcdir/${_pkgfn/opensource-/}/LICENSE* -t "$pkgdir${MINGW_PREFIX}"/share/licenses/${_realname}

  # Seperate debug-info files
  mv -f "${pkgdir}${MINGW_PREFIX}/lib"/*.debug "${pkgdir}${MINGW_PREFIX}/bin"/ || true
  rsync -armR --remove-source-files --include="*/" --include="*.debug" --exclude="*" --prune-empty-dirs ${pkgdir}/.${MINGW_PREFIX} ${srcdir}/${MSYSTEM}-debug/

  # Fix paths in *.pri, *prl and *.pc files:
  #   $(cygpath -m ${MINGW_PREFIX}) -> $(cygpath -m ${pkgdir}${MINGW_PREFIX})
  local PKGDIR_PREFIX_WIN=$(cygpath -m ${pkgdir}${MINGW_PREFIX})
  local FAKE_PREFIX_FOR_REPLACE="@@QT_REAL_PREFIX/dir@@"

  find "${pkgdir}${MINGW_PREFIX}/share/qt5" -type f \( -name '*.pri' -o -name '*.prl' \) \
      -exec sed -i -e "s|${PREFIX_WIN}|${FAKE_PREFIX_FOR_REPLACE}|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/lib" -type f \( -name '*.pri' -o -name '*.prl' \) \
      -exec sed -i -e "s|${PREFIX_WIN}|${FAKE_PREFIX_FOR_REPLACE}|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig" -type f -name '*.pc' \
      -exec sed -i -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig" -type f -name '*.pc' \
      -exec sed -i -e "s|${PKGDIR_PREFIX_WIN}|${MINGW_PREFIX}|g" {} \;
}

package_qt5-3d-debug() {
  depends=("${MINGW_PACKAGE_PREFIX}-qt5-declarative-debug"
           "${MINGW_PACKAGE_PREFIX}-${_realname}")
  groups=("${MINGW_PACKAGE_PREFIX}-qt5-debug")
  options=('!strip')

  cp -rf ${srcdir}/${MSYSTEM}-debug${MINGW_PREFIX} "${pkgdir}"/
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
